<template>
  <el-dialog title="输入学员手机号" :visible="visible" width="400px" top="30vh" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="student" :model="user" size="small">
      <el-form-item prop="phone" :rules="rulesPhone"><edu-input key="phone" :value.sync="user.phone" /></el-form-item>
      <el-form-item>
        <el-row type="flex" justify="end">
          <el-button type="primary" :disabled="disableSubmit" @click="onSubmit">保存</el-button>
          <el-button @click="$emit('update:visible', false)">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import EduInput from '@public/form/input.vue';
export default {
  components: {
    EduInput
  },
  props: {
    courseId: {
      type: [Number, String],
      required: true
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  inject: ['reload'],
  data() {
    return {
      user: {
        phone: ''
      },
      rulesPhone: [
        { required: true, message: '请输入要添加的学员手机号', trigger: ['blur', 'change'] },
        {
          pattern: /^1[3-9]\d{9}$/,
          message: '请输入正确的手机号',
          trigger: ['blur', 'change']
        }
      ],
      disableSubmit: false
    };
  },
  methods: {
    handleClose() {
      this.$emit('update:visible', false);
    },
    onSubmit: function() {
      this.$refs['student'].validate(valid => {
        if (valid) {
          this.disableSubmit = true;
          this.$http.post(
            '/course/admin/' + this.courseId + '/student',
            {
              phone: this.user.phone
            },
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
              this.disableSubmit = false;
            },
            false
          );
        }
      });
    }
  }
};
</script>

<style></style>
